package custom;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.primefaces.component.api.UIData;

public class CurrentPageReportC implements PaginatorElementRendererCustom {

    public void render(FacesContext context, UIData uidata) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        String template = uidata.getCurrentPageReportTemplate();
        int currentPage = uidata.getPage() + 1;
        int pageCount = uidata.getPageCount();
        if(pageCount == 0) {
            pageCount = 1;
        }
                
        String output = template.replaceAll("\\{currentPage\\}", Integer.toString(currentPage))
        .replaceAll("\\{totalPages\\}", Integer.toString(pageCount))
        .replaceAll("\\{totalRecords\\}", Integer.toString(uidata.getRowCount()))
        .replaceAll("\\{startRecord\\}", Integer.toString(Math.min(uidata.getFirst() + 1, uidata.getRowCount())))
        .replaceAll("\\{endRecord}", Integer.toString(Math.min(uidata.getFirst() + uidata.getRowsToRender(), uidata.getRowCount())));
        
        writer.startElement("span", null);
        writer.writeAttribute("class", UIData.PAGINATOR_CURRENT_CLASS, null);
        writer.writeText(output, null);
        writer.endElement("span");
    }
}
